/*
 *  levelUpGame - AStarNode.cpp
 *  Copyright 2011 W. Reckman. All rights reserved.
 */
#include "AStarNode.h"

AStarNode::AStarNode()
: parent( 0 ), pos()
{}

AStarNode::AStarNode( const GridPoint<int>& pos_ )
: parent( 0 ), pos( pos_ )
{}

AStarNode::AStarNode( AStarNode* parent_, const GridPoint<int>& pos_ )
: parent( parent_ ), pos( pos_ )
{}

AStarNode::AStarNode( const AStarNode& other_ )
: parent( other_.parent ), cost( other_.cost ), gCache( other_.gCache ), hEstimate( other_.hEstimate ), pos( other_.pos )
{}

AStarNode* AStarNode::getParent() const { return parent; }
const GridPoint<int>& AStarNode::getPos() const { return pos; }
GridPoint<int>& AStarNode::getPos() { return pos; }
float AStarNode::getCost() const { return cost; }
float AStarNode::getF() const { return gCache + hEstimate; }
float AStarNode::getG() const { return gCache; }
float AStarNode::getH() const { return hEstimate; }

void AStarNode::setParent( AStarNode* parent_ ) { parent = parent_; recalculateG(); }
void AStarNode::setPos( const GridPoint<int>& pos_ ) { pos = pos_; }
void AStarNode::setCost( float cost_ ) { cost = cost_; recalculateG(); }
void AStarNode::setH( float h_ ) { hEstimate = h_; }

bool AStarNode::samePosition( const AStarNode& other_ ) const { return pos == other_.pos; }
bool AStarNode::operator==( const AStarNode& other_ ) const { return samePosition( other_ ); }


/* This function compares the F values of two A* nodes. */
bool AStarNodeFCompare::operator() ( const AStarNode* lhv, const AStarNode* rhv ) const {
	return lhv->getF() > rhv->getF();
}


/*********************
 * RPVIATE FUNCTIONS *
 *********************/

float AStarNode::recalculateG() {
	if ( parent ) gCache = parent->recalculateG() + cost;
	else gCache = cost;
	
	return gCache;
}
